package com.engine4cloud.tdf.supervision.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.engine4cloud.tdf.common.core.constant.enums.DictNameEnum;
import com.engine4cloud.tdf.common.core.exception.ValidateCodeException;
import com.engine4cloud.tdf.supervision.convert.JzdxJudgmentConvert;
import com.engine4cloud.tdf.supervision.convert.JzdxSpecialCrimeConvert;
import com.engine4cloud.tdf.supervision.dto.create.JzdxJudgmentCreateDTO;
import com.engine4cloud.tdf.supervision.dto.query.JzdxSpecialCrimeQueryDTO;
import com.engine4cloud.tdf.supervision.dto.result.DictSelectResultDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxJudgmentResultDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxResultDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxSpecialCrimeResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.JzdxJudgmentUpdateDTO;
import com.engine4cloud.tdf.supervision.entity.JzdxJudgmentEntity;
import com.engine4cloud.tdf.supervision.entity.JzdxSpecialCrimeEntity;
import com.engine4cloud.tdf.supervision.mapper.JzdxJudgmentMapper;
import com.engine4cloud.tdf.supervision.service.DictSelectService;
import com.engine4cloud.tdf.supervision.service.JzdxJudgmentService;
import com.engine4cloud.tdf.supervision.service.JzdxService;
import com.engine4cloud.tdf.supervision.service.JzdxSpecialCrimeService;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * 矫正对象判决信息表
 *
 * @author wenjie
 * @date 2024-03-15 15:08:04
 */
@Service
public class JzdxJudgmentServiceImpl extends ServiceImpl<JzdxJudgmentMapper, JzdxJudgmentEntity> implements JzdxJudgmentService {
    @Resource
    private JzdxSpecialCrimeService specialCrimeService;

    @Resource
    private JzdxService jzdxService;

    @Resource
    private DictSelectService dictSelectService;

    @Override
    public JzdxJudgmentResultDTO findByCode(String code) {
        JzdxJudgmentResultDTO result = this.baseMapper.findByCode(code);
        this.preSpecialCrimeList(result);
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean create(JzdxJudgmentCreateDTO dto) {
        JzdxResultDTO dx = Optional.ofNullable(jzdxService.getByCode(dto.getRybh()))
                .orElseThrow(() -> new ValidateCodeException("矫正对象数据不存在"));

        JzdxJudgmentResultDTO exist = this.baseMapper.findByCode(dto.getRybh());
        if (Objects.nonNull(exist)) {
            throw new ValidateCodeException("数据已存在");
        }
        //保存判决信息
        this.save(JzdxJudgmentConvert.INSTANCE.create(dto, dx.getTenantId()));
        //设置特殊犯罪信息
        List<JzdxSpecialCrimeEntity> specialCrimeEntityList = JzdxSpecialCrimeConvert.INSTANCE.create(dto, dx.getTenantId());
        if (CollectionUtils.isNotEmpty(specialCrimeEntityList)) {
            specialCrimeService.saveBatch(specialCrimeEntityList);
        }

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(JzdxJudgmentUpdateDTO dto) {
        JzdxJudgmentEntity old = Optional.ofNullable(this.baseMapper.selectById(dto.getId()))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));
        this.updateById(JzdxJudgmentConvert.INSTANCE.update(dto));

        //设置特殊犯罪信息
        return specialCrimeService.updateBatch(old, dto);
    }

    /**
     * 设置特殊犯罪信息
     *
     * @param result 查询结果
     */
    private void preSpecialCrimeList(JzdxJudgmentResultDTO result) {
        if (Objects.isNull(result)) {
            return;
        }

        List<JzdxSpecialCrimeResultDTO> specialCrimeList = specialCrimeService.find(new JzdxSpecialCrimeQueryDTO(result.getRybh()));
        //五独字典
        List<DictSelectResultDTO> fiveIndependenceItems = dictSelectService.items(DictNameEnum.FIVE_INDEPENDENCE.getType());
        //五涉字典
        List<DictSelectResultDTO> fiveInvolvementItems = dictSelectService.items(DictNameEnum.FIVE_INVOLVEMENT.getType());
        //四史字典
        List<DictSelectResultDTO> fourHistoryItems = dictSelectService.items(DictNameEnum.FOUR_HISTORY.getType());

        JzdxSpecialCrimeConvert.INSTANCE.convertList(result, specialCrimeList, fiveIndependenceItems, fiveInvolvementItems, fourHistoryItems);
    }
}